package com.fyzk.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tyjc.lottery.R;


public class SureDialog {
	
	private final String TAG = this.getClass().getSimpleName();
	
	private View contentView;
	private TextView tishi1;
	private TextView tishi2;
	private TextView tishi3;
	private TextView btnSure;
//	private TextView btnCancel;
//	private LinearLayout btnCancel_ll;
	private LinearLayout center_ll;

	AlertDialog alertDialog = null;
	
	/**
	 * 提示信息1
	 */
	private String tishiString1 = "";
	/**
	 * 提示信息2
	 */
	private String tishiString2 = "";
	/**
	 * 按钮1（确认）
	 */
	private String btnSureString = "";
	/**
	 * 按钮2（取消）
	 */
	private String btnCancelString = "";

	private Context context;

//	public SureDialog(Context context){
//		this.context = context;
//	}
//
//	/**
//	 * 对话框  构造函数
//	 * @param context  上下文
//	 * @param theme    主题风格
//	 */
//	public SureDialog(Context context, int theme) {
//		this.context = context;
//		init(null);
//	}

	/**
	 * 对话框  构造函数
	 * @param context  上下文
	 * @param listener  按钮单击事件
	 * @param tishiString1  提示信息1
	 * @param tishiString2  提示信息2
	 */
	public SureDialog(Context context, OnDialogOKCancleClickListener listener
			, String tishiString1, String tishiString2,String btnSureString){
		this(context, listener, tishiString1, tishiString2, btnSureString, "");
	}
	
	/**
	 * 对话框  构造函数
	 * @param context  上下文
	 * @param listener  按钮单击事件
	 * @param tishiString1  提示信息1
	 * @param tishiString2  提示信息2
	 */
	public SureDialog(Context context, OnDialogOKCancleClickListener listener
			, String tishiString1, String tishiString2,String btnSureString,String btnCancelString){
		this.context = context;
		this.listener = listener;
		this.tishiString1 = tishiString1;
		this.tishiString2 = tishiString2;
		this.btnSureString = btnSureString;
		this.btnCancelString = btnCancelString;
		init(null);
	}

	public SureDialog(Context context, int theme, View centerView){
		this.context = context;
		init(centerView);
	}

	public SureDialog(Context context, final OnDialogOKCancleClickListener listener,
					  View centerView, String sureStr, String cancelStr) {
		this.listener = listener;
		alertDialog = new AlertDialog.Builder(context).setTitle("提示").setView(centerView).setPositiveButton(sureStr, new DialogInterface.OnClickListener() {


			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(listener != null){
					listener.onOKClick();
					dismiss();
				}
			}
		}).setNegativeButton(cancelStr, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(listener != null){
					listener.onCancleClick();
					dismiss();
				}
			}
		}).create();


	}

	private void init(View centerView){


		contentView = LayoutInflater.from(context).inflate(R.layout.dialogsure, null);
		tishi1 = (TextView)contentView.findViewById(R.id.dialog_tishi1);
		tishi2 = (TextView)contentView.findViewById(R.id.dialog_tishi2);
		tishi3 = (TextView)contentView.findViewById(R.id.dialog_tishi3);
		center_ll = (LinearLayout) contentView.findViewById(R.id.dialog_center_ll);
		
		if (tishiString1.equals("")) {
			tishi3.setVisibility(View.VISIBLE);
		}else {
			tishi3.setVisibility(View.GONE);
		}
		tishi1.setText(""+this.tishiString1);
		tishi2.setText(""+this.tishiString2);

		// 添加自定义布局
		if (centerView != null){
			center_ll.removeAllViews();
			center_ll.addView(centerView);
		}


		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("提示");
		builder.setView(contentView);
		builder.setPositiveButton(btnSureString, new DialogInterface.OnClickListener() {


			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(listener != null){
					listener.onOKClick();
				}
			}
		});

		builder.setNegativeButton(btnCancelString, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(listener != null){
					listener.onCancleClick();
				}
			}
		});

		alertDialog = builder.create();

	}

	public void show() {
		alertDialog.setCanceledOnTouchOutside(false);
		if(!alertDialog.isShowing()){
			alertDialog.show();
		}
	}

	public interface OnDialogOKCancleClickListener{
		void onOKClick();
		void onCancleClick();
	} 
	
	private OnDialogOKCancleClickListener listener = null;
	
	public void setOnDialogOKCancleClickListener(OnDialogOKCancleClickListener listener){
		this.listener = listener;
	}

	public String getTishiString1() {
		return tishiString1;
	}

	public void setTishiString1(String tishiString1) {
		this.tishiString1 = tishiString1;
		tishi1.setText(""+this.tishiString1);
		
		if (tishiString1.equals("")) {
			tishi3.setVisibility(View.VISIBLE);
		}else {
			tishi3.setVisibility(View.GONE);
		}
	}

	public String getTishiString2() {
		return tishiString2;
	}

	public void setTishiString2(String tishiString2) {
		this.tishiString2 = tishiString2;
		tishi2.setText(""+this.tishiString2);
	}

	public String getBtnSureString() {
		return btnSureString;
	}

	public void setBtnSureString(String btnSureString) {
		this.btnSureString = btnSureString;
		btnSure.setText(""+this.btnSureString);
	}
	


	public void dismiss() {
		if (alertDialog !=null && alertDialog.isShowing()) {
			alertDialog.cancel();
		}
	}

	public TextView getBtnSure() {
		return btnSure;
	}

	public void setBtnSure(TextView btnSure) {
		this.btnSure = btnSure;
	}

}
